// pages/productManagement/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList:['分类管理','产品管理'],
    tabIndex:0,
    list:[],
    foodList:[],
    tcList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      if(options.tabIndex){
        this.setData({
          tabIndex:parseInt(options.tabIndex)
        })
      }
  },
  storeProTypeList(){
    let data = {
      id:wx.getStorageSync('userInfo').storeId
    }
    app.agriknow.storeProTypeList(data).then(res=>{
      console.log(res)
      this.setData({
        list:res.data
      })
    })
  },
  gotoAddTab(){
    wx.navigateTo({
      url: '/pages/addTab/index',
    })
  },
  editTab(e){
    console.log(e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/editTab/index?id='+id,
    })
  },
  deleteTab(e){
    let that = this
    let id = e.currentTarget.dataset.id
    let data = {
      id:id
    }
    wx.showModal({
      title: '提示',
      content: '确定删除',
      success (res) {
        if (res.confirm) {
          app.agriknow.storeProDelType(data).then(res=>{
            if(res.code=='000000'){
              that.tipsMessage('删除成功')
              setTimeout(()=>{
                that.init()
              },1000)
            }
          }).catch(res=>{
            that.tipsMessage(res.message)
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  storeProList(){
    let data = {
      id:wx.getStorageSync('userInfo').storeId
    }
    app.agriknow.storeProList(data).then(res=>{
      console.log(res)
      this.setData({
        foodList:res.data
      })
    })
  },
  gotoAddProduct(){
    wx.navigateTo({
      url: '/pages/uploadProduct/index',
    })
  },
  storePackageList(){
    let data = {
      storeId:wx.getStorageSync('userInfo').storeId
    }
    app.agriknow.storePackageList(data).then(res=>{
      console.log(res)
      this.setData({
        tcList:res.data
      })
    })
  },
  toUp:function(e){
    var that=this
    var isListing=e.currentTarget.dataset.isListing
    if(!isListing){
      wx.showModal({
        title: '温馨提示',
        content: '上架后无法编辑，请确定是否上架',
        complete: (res) => {
          if (res.cancel) {
            
          }
      
          if (res.confirm) {
            that.packageListingPro(e)
          }
        }
      })
    }
  },
  packageListingPro(e){
    

    let data = {
      id:e.currentTarget.dataset.id
    }
    app.agriknow.packageListingPro(data).then(res=>{
      if(res.code=='000000'){
        this.tipsMessage('操作成功')
        setTimeout(()=>{
          this.init()
        },1000)
      }else{
        this.tipsMessage(res.message)
      }
      
    })
  },
  gotoDetail(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/uploadProduct/index?id='+id,
    })
  },
  delete(e){
    let id = e.currentTarget.dataset.id
    let data = {
      id:id
    }
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定删除',
      success (res) {
        if (res.confirm) {
          app.agriknow.delProduct(data).then(res=>{
            if(res.code=='000000'){
              that.tipsMessage('删除成功')
              that.init()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  gototc(){
    wx.navigateTo({
      url: '/pages/uploadPackage/index',
    })
  },
  onClick(e){
    console.log(e.currentTarget.dataset.index)
    this.setData({
      tabIndex:e.currentTarget.dataset.index
    })
  },
  storeListingPro(e){
    let data = {
      id:e.currentTarget.dataset.id
    }
    app.agriknow.storeListingPro(data).then(res=>{
      if(res.code=='000000'){
        this.tipsMessage('操作成功')
        setTimeout(()=>{
          this.init()
        },1000)
      }else{
        this.tipsMessage(res.message)
      }
      
    })
  },
  //提示信息
  tipsMessage(text) {
    wx.showToast({
      title: text,
      icon: 'none',
      duration: 2000,
      mask: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  init(){
    this.storeProTypeList()
    this.storeProList()
    this.storePackageList()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  editPackage:function(e){
    var id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/uploadPackage/index?id='+id,
    })
  },

  submit:function(e){
    var vals=e.detail.value
    var list=this.data.foodList
    /*
    if(!vals.keyword){
      wx.showToast({
        title: '请输入关键词',
        icon:'none',
        duration:1500
      })
      return false
    }
    */
    var result=list.filter(item=>{return item.name.includes(vals.keyword)})
    console.log(result)
    if(result.length==0){
      this.setData({
        empty:true
      })
    }else{
      this.setData({
        empty:false
      })
    }
    this.setData({
      keyword:vals.keyword,
      result:result
    })
  

  },
})